package com.uxhao.educenter.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uxhao.commonutils.R;
import com.uxhao.commonutils.entity.vo.UcenterMemberInfoCommonVo;
import com.uxhao.commonutils.util.JwtUtil;
import com.uxhao.educenter.entity.MemberQuery;
import com.uxhao.educenter.entity.UcenterMember;
import com.uxhao.educenter.entity.dashboard.WeekDataVo;
import com.uxhao.educenter.entity.vo.*;
import com.uxhao.educenter.service.UcenterMemberService;
import com.uxhao.servicebase.exceptionhandler.QLLException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author uxhao
 * @since 2022/01/19
 */
@Api(tags={"登录注册接口"})
@RestController
//@CrossOrigin
@RequestMapping("/educenter/ucenter")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        String token = ucenterMemberService.login(loginVo);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtil.getMemberIdByJwtToken(request);
            UserInfoVo loginInfoVo = ucenterMemberService.getLoginInfo(memberId);
            return R.ok().data("item", loginInfoVo);
        }catch (Exception e){
            e.printStackTrace();
            throw new QLLException(20001,"获取用户信息失败");
        }
    }

    /**
     *  [远程调用]根据id获取用户信息
     * @param id 用户id
     * @return 公共模块下的vo
     */
    @ApiOperation(value = "[远程调用]根据id获取用户信息")
    @GetMapping("getInfoUc/{id}")
    public UcenterMemberInfoCommonVo getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember user =ucenterMemberService.getById(id);
        System.out.println(user);
        UcenterMemberInfoCommonVo userInfoVo = new UcenterMemberInfoCommonVo();
        BeanUtils.copyProperties(user, userInfoVo);
        return userInfoVo;
    }

    /**
     * [远程调用]根据日期获取注册人数
     * @param day
     * @return
     */
    @ApiOperation(value = "[远程调用]根据日期获取注册人数")
    @GetMapping(value = "countregister/{day}")
    public Map<String,Object> registerCount(
            @PathVariable String day){
        Integer count = ucenterMemberService.countRegisterByDay(day);
        Map<String,Object> map =new HashMap<>();
        map.put("countRegister",count);
        return map;
    }

    /**
     * [远程调用]获取总注册人数
     * @return
     */
    @ApiOperation(value = "[远程调用]获取总注册人数")
    @GetMapping(value = "countAllRegister")
    public Map<String,Object> countAllRegister(){
        Integer registerNumber = ucenterMemberService.countAllRegister();
        Map<String,Object> map =new HashMap<>();
        map.put("registerNumber",registerNumber);
        return map;
    }

    /**
     * [远程调用]获取上周和本周的会员注册数量
     * @return
     */
    @ApiOperation(value = "[远程调用]获取上周和本周的会员注册数量")
    @GetMapping(value = "countLastAndCurrentWeekRegister")
    public Map<String,Object> countLastAndCurrentWeekRegister(){
        WeekDataVo lastWeekData = ucenterMemberService.countlastWeekRegister();
        WeekDataVo currentWeekData = ucenterMemberService.countCurrentWeekRegister();
        Map<String,Object> map =new HashMap<>();
        map.put("lastWeekData",lastWeekData);
        map.put("currentWeekData",currentWeekData);
        return map;
    }

    @ApiOperation("条件查询带分页方法")
    @PostMapping("pageListMemberCondition/{current}/{limit}")
    public R pageListTeacherCondition(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) MemberQuery memberQuery) {//@RequestBody 使用json封装到参数,需要post请求
        //创建page对象
        Page<UcenterMember> page = new Page<>(current,limit);
        //MP 构建条件
        QueryWrapper<UcenterMember> wrapper =new QueryWrapper<>();
        String nickname = memberQuery.getNickname();
        String email = memberQuery.getEmail();
        String begin = memberQuery.getBegin();
        String end = memberQuery.getEnd();
        if(!StringUtils.isEmpty(nickname))wrapper.like("nickname",nickname);
        if(!StringUtils.isEmpty(email))wrapper.like("email",email);
        if(!StringUtils.isEmpty(begin))wrapper.ge("gmt_create",begin);//大于
        if(!StringUtils.isEmpty(end))wrapper.le("gmt_create", end);//小于
        wrapper.eq("is_deleted",0);//未逻辑删除
        wrapper.orderByDesc("gmt_create");
        ucenterMemberService.page(page,wrapper);
        long total = page.getTotal();//总记录数
        List<UcenterMember> records = page.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "根据id激活用户")
    @PostMapping("active/{memberId}")
    public R active(@PathVariable("memberId") String memberId){
        UcenterMember member = new UcenterMember();
        member.setId(memberId);
        //true 为冻结状态，false为激活状态
        member.setIsDisabled(false);
        boolean b = ucenterMemberService.updateById(member);
        return b?R.ok():R.error();
    }

    @ApiOperation(value = "根据id冻结用户")
    @PostMapping("freeze/{memberId}")
    public R freeze(@PathVariable("memberId") String memberId){
        UcenterMember member = new UcenterMember();
        member.setId(memberId);
        //true 为冻结状态，false为激活状态
        member.setIsDisabled(true);
        boolean b = ucenterMemberService.updateById(member);
        return b?R.ok():R.error();
    }

    @ApiOperation(value = "根据id逻辑删除用户用户")
    @DeleteMapping("delete/{memberId}")
    public R delete(@PathVariable("memberId") String memberId){
        UcenterMember member = new UcenterMember();
        member.setId(memberId);
        //true 为冻结状态，false为激活状态
        member.setIsDeleted(true);
        boolean b = ucenterMemberService.updateById(member);
        return b?R.ok():R.error();
    }
}
